
// Copyright (C) 2007 Richard Uhler
// This file is part of Esiply.
//
// Esiply is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License, Version 2,
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include "HostContext.h"
#include "HostValue.h"
#include "RegularExpressionLiteral.h"
#include "String.h"

RegularExpressionLiteral::RegularExpressionLiteral(String* pattern, String* flags)
    : m_pattern(pattern), m_flags(flags), m_regexp(NULL)
{}

Value* RegularExpressionLiteral::evaluate(Scope* scope)
{
    // If we already have the regexp object, just return it
    if (m_regexp)
    {
        return m_regexp;
    }

    // We need to create the object. This is done simply by calling
    // some javascript: new RegExp(pattern, flags);
    // TODO: check for a syntax error.
    HostContext hc(scope);
    HostValue regexp;
    HostValue pattern(m_pattern);
    HostValue flags(m_flags);
    hc.host_eval("$1 = new RegExp($2, $3);", &regexp, &pattern, &flags);
    m_regexp = regexp.get_value();
    return m_regexp;
};


